package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ColumnMng;
import com.ruoyi.system.domain.OperateStructureDTO;
import com.ruoyi.system.domain.SaveColumnDTO;
import com.ruoyi.system.domain.TableMng;
import com.ruoyi.system.mapper.ColumnMngMapper;
import com.ruoyi.system.mapper.TableMngMapper;
import com.ruoyi.system.service.ITableMngService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 数据Service业务层处理
 *
 * @author weicc
 * @date 2023-09-23
 */
@Service
public class TableMngServiceImpl implements ITableMngService {
    @Resource
    private TableMngMapper tableMngMapper;

    @Resource
    private ColumnMngMapper columnMngMapper;

    /**
     * 查询数据
     *
     * @param id 数据主键
     * @return 数据
     */
    @Override
    public TableMng selectTableMngById(String id) {
        return tableMngMapper.selectTableMngById(id);
    }

    /**
     * 查询数据列表
     *
     * @param tableMng 数据
     * @return 数据
     */
    @Override
    public List<TableMng> selectTableMngList(TableMng tableMng) {
        return tableMngMapper.selectTableMngList(tableMng);
    }

    /**
     * 新增数据
     *
     * @param tableMng 数据
     * @return 结果
     */
    @Override
    public int insertTableMng(TableMng tableMng) {
        tableMng.setCreateTime(DateUtils.getNowDate());
        return tableMngMapper.insertTableMng(tableMng);
    }

    /**
     * 修改数据
     *
     * @param tableMng 数据
     * @return 结果
     */
    @Override
    public int updateTableMng(TableMng tableMng) {
        tableMng.setUpdateTime(DateUtils.getNowDate());
        return tableMngMapper.updateTableMng(tableMng);
    }

    /**
     * 批量删除数据
     *
     * @param ids 需要删除的数据主键
     * @return 结果
     */
    @Override
    public int deleteTableMngByIds(String[] ids) {
        return tableMngMapper.deleteTableMngByIds(ids);
    }

    /**
     * 删除数据信息
     *
     * @param id 数据主键
     * @return 结果
     */
    @Override
    public int deleteTableMngById(String id) {
        return tableMngMapper.deleteTableMngById(id);
    }

    @Override
    public void saveStructure(OperateStructureDTO operateStructureDTO) {
        Date now = new Date();
        TableMng tableMngEntity = new TableMng();
        tableMngEntity.setId(UUID.randomUUID().toString());
        tableMngEntity.setDbId(operateStructureDTO.getDbId());
        tableMngEntity.setTableName(operateStructureDTO.getTableName());
        tableMngEntity.setTableComment(operateStructureDTO.getTableComment());
        tableMngEntity.setDelFlag("0");
        tableMngEntity.setCreateTime(now);
        tableMngEntity.setUpdateTime(now);
        tableMngEntity.setCreateBy("sync");
        tableMngEntity.setUpdateBy("sync");
        this.insertTableMng(tableMngEntity);

        List<ColumnMng> columnMngEntityList = new ArrayList<>();
        for (Map.Entry<String, SaveColumnDTO> columnEntry : operateStructureDTO.getColumnMap().entrySet()) {
            {
                ColumnMng columnMngEntity = new ColumnMng();
                BeanUtils.copyProperties(columnEntry.getValue(), columnMngEntity);
                columnMngEntity.setId(UUID.randomUUID().toString());
                columnMngEntity.setTableId(tableMngEntity.getId());
                columnMngEntity.setDelFlag("0");
                columnMngEntity.setCreateTime(now);
                columnMngEntity.setUpdateTime(now);
                columnMngEntity.setCreateBy("sync");
                columnMngEntity.setUpdateBy("sync");
                columnMngEntityList.add(columnMngEntity);
            }
        }
        if (!CollectionUtils.isEmpty(columnMngEntityList)) {
            columnMngMapper.saveBatch(columnMngEntityList);
        }
    }

    @Override
    public void deleteStructure(OperateStructureDTO value) {
        Optional.ofNullable(columnMngMapper.selectColumnMngById(value.getId())).ifPresent(tableMngEntity -> {
            tableMngEntity.setDelFlag("1");
            columnMngMapper.updateById(tableMngEntity);
        });
    }

    @Override
    public void updateStructure(OperateStructureDTO newTable) {
        Optional.ofNullable(columnMngMapper.selectColumnMngById(newTable.getId())).ifPresent(tableMngEntity -> {
            BeanUtils.copyProperties(newTable, tableMngEntity);
            tableMngEntity.setUpdateTime(new Date());
            columnMngMapper.updateById(tableMngEntity);
        });
    }
}
